package com.atguigu.java;

import org.junit.Test;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @author : leimingchao
 * @date Date : 2021年06月25日 21:01
 */
public class SimpleDateFormatTest {

    /**
     * jdk7
     */
    @Test
    public void test() {
        // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        // Callable<Date> task = () -> simpleDateFormat.parse("20161218");
        Callable<Date> task = () -> DateFormatThreadLocal.convert("20161218");
        ExecutorService executorService = new ThreadPoolExecutor(3, 10, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        List<Future<Date>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(executorService.submit(task));
        }
        list.stream().forEach(e -> {
            try {
                System.out.println(e.get());
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                e1.printStackTrace();
            }
        });
        executorService.shutdown();
    }

    /**
     * jdk8
     */
    @Test
    public void test2() {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        Callable<LocalDate> task = () -> LocalDate.parse("20181218",dateTimeFormatter);
        ExecutorService executorService = new ThreadPoolExecutor(3, 10, 3, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy());
        List<Future<LocalDate>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(executorService.submit(task));
        }
        list.stream().forEach(e -> {
            try {
                System.out.println(e.get());
            } catch (InterruptedException e1) {
                e1.printStackTrace();
            } catch (ExecutionException e1) {
                e1.printStackTrace();
            }
        });
        executorService.shutdown();
    }
}
